package xxdsqljobDB

import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.support.PropertiesLoaderUtils
import org.springframework.jdbc.datasource.DriverManagerDataSource

import java.sql.Clob

/**
 * Created by Administrator on 2019-07-02.
 */
class DBUtil {
    static getGroovySqlInstance(){
        def property = getDBConfig()
        def ds = new DriverManagerDataSource(
                property.get("dataSource.driverClassName"),
                property.get("dataSource.url"),
                property.get("dataSource.username"),
                property.get("dataSource.password"))
        return new groovy.sql.Sql(ds)
    }

    static getDBConfig(){
        return PropertiesLoaderUtils.loadProperties(new ClassPathResource("dataSource.properties"))
    }

    /**
     * java.sql.Clob ת String
     * @param clob
     * @return
     */
    static Clob2String(Object clob){
        StringBuffer stringBuffer = new StringBuffer("")

        if( clob instanceof java.sql.Clob || clob instanceof oracle.sql.CLOB){
            if(clob){
                Reader reader =  clob.getCharacterStream()
                BufferedReader bufferedInputStream = new BufferedReader(reader)
//            println "bufferedInputStream.ready():${bufferedInputStream.ready()}"
                def s = bufferedInputStream.readLine()
                while (s!=null){
                    stringBuffer.append(s)
                    s = bufferedInputStream.readLine()
                }
                bufferedInputStream.close()
                reader.close()
            }
        }else{
            stringBuffer.append(String.valueOf(clob))
        }
        return stringBuffer.toString()
    }
}
